package com.phy.gsjl.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.phy.gsjl.entity.User;
import com.phy.gsjl.service.IUserService;

/**
 * 用户管理控制器
 * 
 * @author curry
 *
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

	@Autowired
	public IUserService userServiceImpl;
	
	//获取日志对象
	private static Logger logger = Logger.getLogger(UserController.class);

	/**
	 * 更新用户信息
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/update")
	public String update(User user) {
		logger.info("==>update info of user:");
		logger.info(user.toString());
		// 调用Service层的更新方法，返回更新成功与否标识
		boolean isUpdate = userServiceImpl.updateUser(user);
		if (isUpdate) {
			return "UpdateSuccess";
		} else {
			return "UpdateFailed";
		}
	}

	/**
	 * 删除单个用户
	 * 
	 * @param username
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public String deleteUser(String username) {
		logger.info("==>delete username:" + username);
		// 调用Service层的删除方法，返回更新成功与否标识
		boolean isDelete = userServiceImpl.deleteUser(username);
		if (isDelete) {
			return "DeleteSuccess";
		} else {
			return "DeleteFailed";
		}
	}

	/**
	 * 批量删除用户
	 * 
	 * @param username
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deleteBatch")
	public String deleteUserByBatch(String[] usernames) {
		logger.info("==>deleteBatch info of usernames: " + usernames.toString());
		// 调用Service层的删除方法，返回删除成功与否标识
		boolean isDelete = userServiceImpl.deleteUserByBatch(usernames);
		if (isDelete) {
			return "DeletesSuccess";
		} else {
			return "DeletesFailed";
		}
	}

	/**
	 * 根据真实姓名模糊查询
	 * 
	 * @param type
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/indistinct", method = RequestMethod.GET)
	public List<User> getUserIndistinct(String indistinct) {
		logger.info("==>indistinct info of the indistinct:" + indistinct);
		// 调用Service层的查询方法
		List<User> users = userServiceImpl.getUserIndistinct(indistinct);
		return users;
	}

	/**
	 * 显示所有用户信息
	 * 
	 * @param type
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/lists", method = RequestMethod.GET)
	public List<User> getMessages() {
		// 调用Service层的查询方法
		List<User> users = userServiceImpl.getAllUsers();
		return users;
	}

	/**
	 * 显示所有用户信息(分页)
	 * 
	 * @param type
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public List<User> getMessageByPage(Integer page) {
		// 调用Service层的查询方法
		List<User> users = userServiceImpl.getMessageByPage(page);
		return users;
	}

	/**
	 * 获取总记录数
	 * 
	 * @param type
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/records", method = RequestMethod.POST)
	public Integer getTotalRecords() {
		// 调用Service层的查询方法
		return userServiceImpl.getTotalRecords();
	}

	/**
	 * 单个添加用户
	 * @param user
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/addUser", method = RequestMethod.POST,produces={"text/html;charset=UTF-8;","application/json;"})
	public String addUser(User user) {
		logger.info("==>addUser info of user:" + user.toString());
		// 调用Service层的添加方法
		boolean isAdded = userServiceImpl.addUser(user);
		if (isAdded) {
			return "添加成功";
		} else {
			return "添加失败";
		}
	}
	
	/**
	 * 文件上传并解析，批量添加用户
	 * 
	 * @param file
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "/fileupload", method = RequestMethod.POST,produces={"text/html;charset=UTF-8;","application/json;"})
	public String doUploadFile(@RequestParam("picpath") MultipartFile file,HttpServletResponse response) throws IOException {
		String path = "upload" + new Date().getTime() + file.getOriginalFilename();//将文件存在eclipse的工作空间目录下
		logger.info("==>fileupload path:" + path);
		//判断文件名是否为空，以及判断文件类型
		if (!file.isEmpty()) {
			if(!path.endsWith("xls")&&!path.endsWith("xlsx")){
				return "文件格式错误！";
			}else {
				//将文件上传
				File newFile = new File(path);
				file.transferTo(newFile);
				//也可以使用以下方法存储文件
				//FileUtils.copyInputStreamToFile(file.getInputStream(), new File("/file", "user.xsls"));
				// 解析excel
				String isadduser = userServiceImpl.addUsers(path);
				if (isadduser.equals("1")) {
					return "上传成功！";
				}else if(isadduser.equals("0")) {
					return "文档内容错误！";
					
				}else if(isadduser.equals("2")) {
					return "文档格式错误！";
					
				}else {
					return "创建流失败！";
				}
			} 
		}else {
			return "文件不存在";	
		}
	}
}
